package com.tfxy.hxl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfxy.hxl.entity.Course;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 课程Mapper接口
 */
@Mapper
public interface CourseMapper extends BaseMapper<Course> {

    /**
     * 分页查询课程列表（带条件筛选）
     * 
     * @param page 分页参数
     * @param categoryId 分类ID
     * @param difficultyLevel 难度等级
     * @param minPrice 最小价格
     * @param maxPrice 最大价格
     * @param keyword 关键词
     * @return 课程分页列表
     */
    IPage<Course> selectCoursePageWithConditions(
            Page<Course> page,
            @Param("categoryId") Long categoryId,
            @Param("difficultyLevel") Integer difficultyLevel,
            @Param("minPrice") BigDecimal minPrice,
            @Param("maxPrice") BigDecimal maxPrice,
            @Param("keyword") String keyword
    );

    /**
     * 查询热门课程（按报名数量排序）
     * 
     * @param limit 限制数量
     * @return 热门课程列表
     */
    @Select("SELECT * FROM course WHERE status = 1 AND deleted = 0 ORDER BY enroll_count DESC, rating DESC LIMIT #{limit}")
    List<Course> selectPopularCourses(@Param("limit") Integer limit);

    /**
     * 查询高评分课程
     * 
     * @param minRating 最小评分
     * @param limit 限制数量
     * @return 高评分课程列表
     */
    @Select("SELECT * FROM course WHERE status = 1 AND deleted = 0 AND rating >= #{minRating} AND rating_count >= 5 ORDER BY rating DESC, rating_count DESC LIMIT #{limit}")
    List<Course> selectHighRatedCourses(@Param("minRating") BigDecimal minRating, @Param("limit") Integer limit);

    /**
     * 根据分类查询课程
     * 
     * @param categoryId 分类ID
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT * FROM course WHERE category_id = #{categoryId} AND status = 1 AND deleted = 0 ORDER BY rating DESC, enroll_count DESC LIMIT #{limit}")
    List<Course> selectCoursesByCategory(@Param("categoryId") Long categoryId, @Param("limit") Integer limit);

    /**
     * 根据难度等级查询课程
     * 
     * @param difficultyLevel 难度等级
     * @param limit 限制数量
     * @return 课程列表
     */
    @Select("SELECT * FROM course WHERE difficulty_level = #{difficultyLevel} AND status = 1 AND deleted = 0 ORDER BY rating DESC LIMIT #{limit}")
    List<Course> selectCoursesByDifficulty(@Param("difficultyLevel") Integer difficultyLevel, @Param("limit") Integer limit);

    /**
     * 更新课程浏览次数
     * 
     * @param courseId 课程ID
     * @return 更新结果
     */
    @Update("UPDATE course SET view_count = view_count + 1 WHERE id = #{courseId}")
    int incrementViewCount(@Param("courseId") Long courseId);

    /**
     * 更新课程报名次数
     * 
     * @param courseId 课程ID
     * @return 更新结果
     */
    @Update("UPDATE course SET enroll_count = enroll_count + 1 WHERE id = #{courseId}")
    int incrementEnrollCount(@Param("courseId") Long courseId);

    /**
     * 更新课程评分信息
     * 
     * @param courseId 课程ID
     * @param rating 新的平均评分
     * @param ratingCount 新的评分人数
     * @return 更新结果
     */
    @Update("UPDATE course SET rating = #{rating}, rating_count = #{ratingCount} WHERE id = #{courseId}")
    int updateRating(@Param("courseId") Long courseId, @Param("rating") BigDecimal rating, @Param("ratingCount") Integer ratingCount);
}
